package org.chenbingkang.aviator;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.security.MessageDigest;

public class AviatorFunc {

    private static ObjectMapper objectMapper=new ObjectMapper();
    static {
        objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
    }

    public static String writeValueAsString(Object o){
        String s=null;
        try {
            s=objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }

    public static String getMD5(String message) {
        MessageDigest messageDigest = null;
        StringBuffer md5StrBuff = new StringBuffer();
        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(message.getBytes("UTF-8"));
            byte[] byteArray = messageDigest.digest();
            for (int i = 0; i < byteArray.length; i++) {
                String t = Integer.toHexString(0xFF & byteArray[i]);
                if (t.length() == 1) {
                    md5StrBuff.append("0");
                }
                md5StrBuff.append(t);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5StrBuff.toString().toLowerCase();// 字母小写
    }
}
